﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StudentManage.Data;
using StudentManage.Common;

namespace StudentManage.Web.UserControl
{
    public partial class UC_Login : System.Web.UI.UserControl
    {
        private int _showId = 0;
        public int ShowId
        {
            set {_showId = value;}
        }

        private void LoadData(UserList md)
        {
            this.lab_UName.Text = md.UName;
            this.Lab_UName_1.Text = md.UName;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Pan_0.Visible = (_showId == 0);
            this.Pan_1.Visible = (_showId == 1);

            if (Session["UserMD"] != null)
                this.LoadData((UserList)Session["UserMD"]);
        }

        protected void Btn_Login_Click(object sender, EventArgs e)
        {
            string UserName = this.UserName.Text.Trim();
            string PassWord = this.PassWord.Text.Trim();
            UserList md = LinqUtility.LinqContext.UserList.Where("UserName=@0 and PassWord=@1", UserName, Utility.GetMD5PassWord(PassWord, "")).FirstOrDefault();
            if (!Convert.ToBoolean(md.IsStudent))
            {
                this.Login_Error.Visible = true;
                return;
            }

            if (md != null)
            {
                Session["UserMD"] = md;
                xy_xsxx info = xy_xsxx.Where("XH=@0", md.XH).FirstOrDefault();//初始化设置所属学院
                if (info != null)
                {
                    xydm xy = xydm.Where("XYMC=@0", info.XY).FirstOrDefault();
                    if (xy != null)
                    {
                        Session["SelectedXYDM"] = xy.XYDM;
                        UC_MasterDrop uc = Page.Controls.AllOfType<UC_MasterDrop>().FirstOrDefault();
                        if (uc != null)
                        {
                            uc.SetValue = xy.XYDM;
                        }
                    }
                }

                this.LoadData(md);
            }
            else
                this.Login_Error.Visible = true;
        }

        protected void Btn_Login_1_Click(object sender, EventArgs e)
        {
            string UserName = this.UserName_1.Text.Trim();
            string PassWord = this.PassWord_1.Text.Trim();
            UserList md = LinqUtility.LinqContext.UserList.Where("UserName=@0 and PassWord=@1", UserName, Utility.GetMD5PassWord(PassWord, "")).FirstOrDefault();
            if (!Convert.ToBoolean(md.IsStudent))
            {
                this.Login_Error_1.Visible = true;
                return;
            }

            if (md != null)
            {
                Session["UserMD"] = md;

                xy_xsxx info = xy_xsxx.Where("XH=@0", md.XH).FirstOrDefault(); //初始化设置所属学院
                if (info != null)
                {
                    xydm xy = xydm.Where("XYMC=@0", info.XY).FirstOrDefault();
                    if (xy != null)
                    {
                        Session["SelectedXYDM"] = xy.XYDM;
                        UC_MasterDrop uc = Page.Controls.AllOfType<UC_MasterDrop>().FirstOrDefault();
                        if (uc != null)
                        {
                            uc.SetValue = xy.XYDM;
                        }
                    }
                }

                this.LoadData(md);
            }
            else
                this.Login_Error_1.Visible = true;
        }
    }
}